package com.tianma315.wx.base;


//                   _ooOoo_
//                  o8888888o
//                  88" . "88
//                  (| -_- |)
//                  O\  =  /O
//               ____/`---'\____
//             .'  \\|     |//  `.
//            /  \\|||  :  |||//  \
//           /  _||||| -:- |||||-  \
//           |   | \\\  -  /// |   |
//           | \_|  ''\---/''  |   |
//           \  .-\__  `-`  ___/-. /
//         ___`. .'  /--.--\  `. . __
//      ."" '<  `.___\_<|>_/___.'  >'"".
//     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//     \  \ `-.   \_ __\ /__ _/   .-` /  /
//======`-.____`-.___\_____/___.-`____.-'======
//                   `=---='
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//         佛祖保佑       永无BUG

import com.tianma315.core.common.exception.MessageException;

import com.tianma315.core.merchant.service.MerchantService;
import com.tianma315.core.sys.domain.UserDO;

import com.tianma315.wx.service.UserServices;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.social.security.SocialUser;

/**
 * 认证控制器，提供用户身份验证
 * <p>
 * Created by zcm on 2018/5/14.
 */
public class AuthenticRest {

    @Autowired
    private UserServices userService;
//    @Autowired
//    private WarehouseManagerService warehouseManagerService;
    @Autowired
    private MerchantService merchantService;

    /**
     * 获取当前登陆用户
     *
     * @return
     */
    protected final SocialUser getUserDO() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication == null || authentication.getPrincipal() == null) {
            throw new MessageException("用户信息获取失败");
        }

        if (authentication.getPrincipal() instanceof SocialUser) {
            return (SocialUser) authentication.getPrincipal();
        }
        throw new MessageException("用户信息获取失败");
    }

    protected UserDO getUser() {
        UserDO userDo = userService.getUserDOByUserName(getUserDO().getUserId());
        if (userDo == null)
            throw new MessageException("用户信息获取失败");
        return userDo;
    }

    /**
     * 获取仓管信息
     *
     * @return
     */
//    protected WarehouseManager getWarehouseManager() {
//        WarehouseManagerVo warehouseManager = warehouseManagerService.getByUserId(getUser().getUser_id());
//        if (warehouseManager == null)
//            throw new MessageException("管理员信息获取失败");
//        return warehouseManager;
//    }


//    protected MerchantDO getMerchant() {
//        MerchantDO merchant = merchantService.getById(getWarehouseManager().getMerchant_id());
//        if (merchant == null)
//            throw new MessageException("商户信息获取失败");
//        return merchant;
//    }


}
